package com.education.controller.backend;

import com.education.annotation.AuthorityAnnotation;
import com.education.common.Const;
import com.education.common.JsonData;
import com.education.pojo.Admin;
import com.education.pojo.Function;
import com.education.pojo.Role;
import com.education.service.IAdminService;
import com.education.service.IDbaService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 林心晨
 * @Title:
 * @Package
 * @Description:
 * @date create in 4.28  15:25
 */
@Controller
@RequestMapping("/dba/")
@AuthorityAnnotation(authority = 0)
public class DBAController {

    @Autowired
    private IDbaService iDbaService ;

    @Autowired
    private IAdminService iAdminService;
    //1.角色管理
    /**
     * 转发到admin-role.ftl页面  角色管理
     * @param model
     * @return
     */
    @RequestMapping(value = "admin_rolePage",method = RequestMethod.GET)
    public ModelAndView adminRolePage(HttpSession session , ModelAndView model){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        model.setViewName("admin-role");
        return model ;
    }

    /**
     * 获取角色信息接口
     * @param session
     * @return
     */
    @RequestMapping(value = "admin_role",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> adminRole(HttpSession session,
                                        @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        Map<String,Object> map ;
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            map = Maps.newHashMap();
            map.put("msg","管理员未登录");
            return map ;
        }
        /* 获取角色对应的用户 */
        PageInfo roleAdmin = iDbaService.getRoleAdmin(pageNum, pageSize, admin.getRoleId());
        if (Const.AdminProfileType.DBA.getCode() != admin.getRoleId()){
            map = Maps.newHashMap();
            map.put("msg","没有操作权限");
            return map ;
        }
        map = Maps.newLinkedHashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",roleAdmin.getTotal());
        map.put("data",roleAdmin.getList());
        return map ;
    }

    //2.权限管理
    /**
     * 转发到admin-role-add.ftl页面
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "admin_role_add",method = RequestMethod.GET)
    public ModelAndView adminRoleAddPage(HttpSession session , ModelAndView model){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
        }
        Integer adminId = admin.getaId();
        if (iDbaService.isSuperRole(adminId)){
            List<Function> functionList = iDbaService.functionList(admin.getRoleId());
            model.addObject("functionList",functionList);
        }
        model.setViewName("admin-role-add");
        return model;
    }

    @RequestMapping(value = "role_add" , method = RequestMethod.POST)
    @ResponseBody
    public JsonData roleAdd(Role role, int[] fIds , String[] urls){
        if (role == null || fIds.length == 0 || urls.length == 0){
            return JsonData.fail("请求参数有空值，请重新检查后在请求");
        }
        return JsonData.success();
    }

    //3.管理员列表
    @RequestMapping(value = "admin_listPage",method = RequestMethod.GET)
    public ModelAndView adminListPage(ModelAndView model,HttpSession session){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        model.setViewName("admin-list");
        return model ;
    }

    @RequestMapping(value = "admin_list",method = RequestMethod.GET)
    @ResponseBody
    public Map adminList(HttpSession session ,
                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
        Map<String,Object> map = null;
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
                map = Maps.newHashMap();
                map.put("msg","请管理员登录");
                return map ;
        }
        /* 可以判断权限 */
        PageInfo pageInfo = iDbaService.meteAdminList(pageNum, pageSize);
        if (pageInfo != null){
            map = Maps.newLinkedHashMap();
            map.put("code",0);
            map.put("msg","");
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            return map ;
        }
        return map ;
    }

    /**
     * 转发到管理员回收站
     * @param model
     * @return
     */
    @RequestMapping(value = "recycle_page",method = RequestMethod.GET)
    public ModelAndView recycle(ModelAndView model){
        model.setViewName("admin-recycle");
        return model ;
    }

    @RequestMapping(value = "recycle_list",method = RequestMethod.GET)
    @ResponseBody
    public Map recycleList(HttpSession session ,
                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
        Map<String,Object> map = null;
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            map = Maps.newHashMap();
            map.put("msg","请管理员登录");
            return map ;
        }
        /* 可以判断权限 */
        PageInfo pageInfo = iDbaService.deletedList(pageNum, pageSize);
        if (pageInfo != null){
            map = Maps.newLinkedHashMap();
            map.put("code",0);
            map.put("msg","");
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            return map ;
        }
        return map ;
    }

    @RequestMapping(value = "admin_open" , method = RequestMethod.POST)
    @ResponseBody
    public JsonData admin_open(@RequestParam(value = "id")Integer id ,
                               @RequestParam(value = "deleted")Integer deleted){
        if (id == null || deleted == null ){
            return JsonData.fail("传入参数异常");
        }
        return iDbaService.deletedChange(id, deleted);
    }

    @RequestMapping(value = "del" , method = RequestMethod.POST)
    @ResponseBody
    public JsonData del(@RequestParam(value = "id")Integer id){
        if (id == null){
            return JsonData.fail("传入参数异常");
        }
        return iDbaService.del(id);
    }

    @RequestMapping(value="role_del",method = RequestMethod.POST)
    @ResponseBody
    public JsonData roleDel(@RequestParam(value = "id")Integer id){
        if (id == null){
            return JsonData.fail("传入参数异常");
        }
        return iDbaService.roleDelete(id);
    }

    /*@RequestMapping("role_edit")
    public ModelAndView role_edit(ModelAndView model){
        //转发到角色编辑页面，该页面会显示该角色的对应信息，并可以修改对应信息
        model.setViewName("");
        return model ;
    }*/

    @RequestMapping(value = "condition_query",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> conditionQuery(HttpSession session,
                                             @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                             @RequestParam(value = "dateMin",required = false)String dateMin,
                                             @RequestParam(value = "dateMax",required = false)String dateMax,
                                             @RequestParam(value = "adminName",required = false)String adminName){
        Map<String,Object> queryMap = null;
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            queryMap = Maps.newHashMap();
            queryMap.put("msg","请管理员登录");
            return queryMap ;
        }
        PageInfo pageInfo = iDbaService.conditionQuery(pageNum, pageSize, dateMin, dateMax, adminName);
        if (pageInfo != null){
            queryMap = Maps.newLinkedHashMap();
            queryMap.put("code",0);
            queryMap.put("msg","");
            queryMap.put("count",pageInfo.getTotal());
            queryMap.put("data",pageInfo.getList());
            return queryMap ;
        }
        return queryMap ;
    }

    @RequestMapping(value = "admin_deleteOne",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteOne(@RequestParam("id") String id){
        Map<String,Object> map ;
        if (StringUtils.isBlank(id)){
            map = Maps.newHashMap();
            map.put("msg","需要传入参数");
        }
        return iDbaService.deleteAdminOne(id);
    }

    /**
     * 跳转到admin-add页面
     * @param model
     * @return
     */
    @RequestMapping(value = "admin-addPage",method = RequestMethod.GET)
    public ModelAndView addAdminPage(ModelAndView model,HttpSession session){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        model.setViewName("admin-add");
        return model ;
    }

    /**
     * 添加管理员接口
     * @param admin
     * @return
     */
    @RequestMapping(value = "admin_add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> adminAdd(HttpSession session ,Admin admin){
        Map<String,Object> map ;
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            map = Maps.newHashMap();
            map.put("msg","请管理员登录");
            return map ;
        }
        return iDbaService.adminAdd(admin);
    }

    /**
     * 批量删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "admin_delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(@RequestParam(value = "id")String[] id,HttpSession session){
        Map<String,Object> map ;
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            map = Maps.newHashMap();
            map.put("msg","请管理员登录");
            return map ;
        }
        return iDbaService.delete(id);
    }

    /**
     * 更改管理员状态接口
     * @param session
     * @param id
     * @param adminState
     * @return
     */
    @RequestMapping(value = "admin_state",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> state(HttpSession session ,
                                    @RequestParam(value = "id")String id,
                                    @RequestParam(value = "adminState") String adminState){
        Map<String,Object> map ;
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            map = Maps.newHashMap();
            map.put("msg","请管理员登录");
            return map ;
        }
        return iDbaService.adminState(id, adminState);
    }

    /**
     * 转发到编辑管理员页面
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value = "admin_editPage/{id}",method = RequestMethod.GET)
    public ModelAndView adminEdit(HttpSession session , ModelAndView model ,@PathVariable(value = "id") String id){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        Admin adminInfo = iDbaService.getAdminInfo(id);
        List<Role> roleList = iDbaService.getRole();
        model.addObject("admin",adminInfo);
        model.addObject("roleList",roleList);
        model.setViewName("admin-edit");
        return model ;
    }

    /**
     *
     * @param session
     * @param aName
     * @param aUsername
     * @param roleId
     * @return
     */
    @RequestMapping(value = "admin_edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(HttpSession session,
                                   @RequestParam(value = "aName",required = false)String aName,
                                   @RequestParam(value = "aUsername",required = false)String aUsername,
                                   @RequestParam(value = "roleId",required = false)String roleId){
        Map<String,Object> map ;
        Admin currentAdmin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (currentAdmin == null){
            map = Maps.newHashMap();
            map.put("msg","请管理员登录");
            return map ;
        }
        return iDbaService.update(aName, aUsername, roleId);
    }

    /**
     * 转发到admin-permission.ftl页面
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "admin_permissionPage",method = RequestMethod.GET)
    public ModelAndView adminPermission(HttpSession session , ModelAndView model){
        Admin admin = (Admin)session.getAttribute(Const.CURRENT_Admin);
        if (admin == null){
            model.addObject("msg","管理员未登录");
            return model ;
        }
        model.setViewName("admin-permission");
        return model ;
    }


}
